// DFileAnaWin.cpp : DFileAna main window
// Copyright (C) 2010, drangon <drangon.zhou@gmail.com>
// 2010-09
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>

#include "DFileAnaWin.h"
#include "FileHexWidget.h"
#include "FileInfoWidget.h"

#include <QMessageBox>
#include <QMenu>
#include <QMenuBar>
#include <QVBoxLayout>
#include <QSplitter>

#include <QDragEnterEvent>
#include <QDropEvent>
#include <QUrl>
#include <QMimeData>
#include <QFileDialog>

DFileAnaWin::DFileAnaWin()
{
	create_menu();

	QSplitter * spl = new QSplitter( Qt::Horizontal );
	m_widhex = new FileHexWidget();
	m_widinfo = new FileInfoWidget();
	spl->addWidget( m_widhex );
	spl->addWidget( m_widinfo );
	spl->setStyleSheet( "QSplitter::handle { background-color: gray }" );
//	spl->setHandleWidth( 3 );
//	spl->setStretchFactor( 1, 1 );
	setCentralWidget( spl );

	spl->setAcceptDrops( false );
	setAcceptDrops( true );
}

DFileAnaWin::~DFileAnaWin()
{
}

int DFileAnaWin::create_menu()
{
	QMenu * file_m = menuBar()->addMenu( "&File" );
	QAction * act = new QAction( "&Open File", this );
	connect( act, SIGNAL(triggered()), this, SLOT(do_open()) );
	file_m->addAction( act );
	file_m->addSeparator();

	act = new QAction( "&Exit", this );
	connect( act, SIGNAL(triggered()), this, SLOT(close()) );
	file_m->addAction( act );

	QMenu * help_m = menuBar()->addMenu( "&Help" );
	act = new QAction( "&About", this );
	connect( act, SIGNAL(triggered()), this, SLOT(do_about()) );
	help_m->addAction( act );

	return 0;
}

int DFileAnaWin::do_open()
{
	// TODO : open file
	QString fname = QFileDialog::getOpenFileName( this, "Open File" );
	m_widhex->SetName( fname );
	return 0;
}

int DFileAnaWin::do_about()
{
	QMessageBox::information( this, "About dfileana", 
			"dfileana : drangon's file analysis tool.\n"
			"copyright (c) drangon.zhou@gmail.com" );
	return 0;
}

void DFileAnaWin::dragEnterEvent( QDragEnterEvent * event )
{
	if( event->mimeData()->hasUrls() )
		event->acceptProposedAction();
	return;
}

void DFileAnaWin::dropEvent( QDropEvent * event )
{
	QList<QUrl> urls = event->mimeData()->urls();
	if( urls.isEmpty() )
		return;
	QString fname = urls.first().toLocalFile();
	if( fname.isEmpty() )
		return;

	// TODO : open file
	m_widhex->SetName( fname );
	return;
}

